/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import bean.Autor;
import bean.Metaavaliacao;
import bean.Noticia;
import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author jorgepereira
 */
public class MetaavaliacaoDAO implements Serializable {

    private final Conexao conexao;
    private PreparedStatement ps;
    private boolean sucess = false;
    private ResultSet rs;

    public MetaavaliacaoDAO() {
        this.conexao = new ConexaoPostgres();

    }

    public Metaavaliacao selecionar(int id) {
        Metaavaliacao ma = new Metaavaliacao();

        String query = "SELECT * FROM metaavaliacao WHERE id = ?";
        try {
            System.out.println("Id:" + id);
            ps = conexao.getConnection().prepareStatement(query);
            ps.setInt(1, id);
            rs = ps.executeQuery();
            if (rs.next()) {
                ma.setId(rs.getInt("id"));
                ma.setTitulo(rs.getString("titulo"));
                ma.setTipo(rs.getString("tipo"));
                ma.setAno(rs.getInt("ano"));
                ma.setDescricao(rs.getString("descricao"));
                ma.setEquipe(rs.getString("equipe"));
                ma.setLink(rs.getString("link"));
                ma.setLocal(rs.getString("local"));
                ma.setUf(rs.getString("uf"));
                PDFDao pdfD = new PDFDao();
                ma.setAnexo1(pdfD.consultarNomePDF(rs.getInt("anexo1")));
                ma.setAnexo2(pdfD.consultarNomePDF(rs.getInt("anexo2")));
                ma.setAnexo3(pdfD.consultarNomePDF(rs.getInt("anexo3")));

            }
        } catch (SQLException ex) {
            conexao.closeConnection();
            Logger.getLogger(MetaavaliacaoDAO.class.getName()).log(Level.SEVERE, null, ex);
        }

        conexao.closeConnection();
        return ma;

    }

    public boolean alterar(Noticia noticia) {
        boolean retorno = false;
        String update = "UPDATE noticia SET resumo=?, texto=?, titulo=?, imagem=?, data=? WHERE id = ?;";

        return retorno;
    }

    public boolean cadastrarAlterar(Metaavaliacao ma) {
        boolean retorno = false;
        String consulta;
        if (ma.getId() > 0) {
            consulta = "UPDATE metaavaliacao SET titulo=?, tipo=?, local=?,  uf=?, ano=?, descricao=?, link=?, equipe=?, anexo1=?, anexo2=?, anexo3=? WHERE id = ?;";

        } else {
            consulta = "INSERT INTO metaavaliacao(titulo, tipo, local, uf, ano, descricao, link, equipe, anexo1, anexo2, anexo3) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";

        }

        sucess = false;

        try {

            ps = conexao.getConnection().prepareStatement(consulta);
            ps.setString(1, ma.getTitulo());
            ps.setString(2, ma.getTipo());
            ps.setString(3, ma.getLocal());
            ps.setString(4, ma.getUf());
            ps.setInt(5, ma.getAno());
            ps.setString(6, ma.getDescricao());
            ps.setString(7, ma.getLink());
            ps.setString(8, ma.getEquipe());

            if (ma.getId() > 0) {
                if (ma.getAnexo1()== null) {
                    ps.setNull(9, Types.NULL);
                } else if(ma.getAnexo1().getId() > 0){
                    ps.setInt(9, ma.getAnexo1().getId());
                    
                }else if (!ma.getAnexo1().getNome().equals("")) {
                    ps.setInt(9, new PDFDao().cadastrarPDF(ma.getAnexo1()));

                } 

                if (ma.getAnexo2()== null) {
                    ps.setNull(10, Types.NULL);
                } else if(ma.getAnexo2().getId() > 0){
                    ps.setInt(10, ma.getAnexo2().getId());
                    
                }else if (!ma.getAnexo2().getNome().equals("")) {
                    ps.setInt(10, new PDFDao().cadastrarPDF(ma.getAnexo2()));

                } 

                 if (ma.getAnexo3()== null) {
                    ps.setNull(11, Types.NULL);
                } else if(ma.getAnexo3().getId() > 0){
                    ps.setInt(11, ma.getAnexo3().getId());
                    
                }else if (!ma.getAnexo3().getNome().equals("")) {
                    ps.setInt(11, new PDFDao().cadastrarPDF(ma.getAnexo3()));

                } 

                ps.setInt(12, ma.getId());
            }else{ 
                
                if (!ma.getAnexo1().getNome().equals("")) {
                    ps.setInt(9, new PDFDao().cadastrarPDF(ma.getAnexo1()));

                } else {
                    ps.setNull(9, Types.NULL);
                }
                
                if (!ma.getAnexo2().getNome().equals("")) {
                    ps.setInt(10, new PDFDao().cadastrarPDF(ma.getAnexo2()));

                } else {
                    ps.setNull(10, Types.NULL);
                }
                
                if (!ma.getAnexo3().getNome().equals("")) {
                    ps.setInt(11, new PDFDao().cadastrarPDF(ma.getAnexo3()));

                } else {
                    ps.setNull(11, Types.NULL);
                }
            }
            ps.executeUpdate();
            conexao.closeConnection();

            sucess = true;
        } catch (SQLException ex) {
            conexao.closeConnection();
            Logger.getLogger(InstituicaoDao.class.getName()).log(Level.SEVERE, null, ex);
            return sucess;
        }

        conexao.closeConnection();
        return sucess;

    }
    
    public List<Metaavaliacao> buscarTodas() {
        ArrayList<Metaavaliacao> metaavaliacaoes = new ArrayList<Metaavaliacao>();

        String query = "SELECT id, titulo, tipo, ano, anexo1, anexo2, anexo3 from metaavaliacao order by ano";
        try {

            ps = conexao.getConnection().prepareStatement(query);
            rs = ps.executeQuery();

            while (rs.next()) {
                Metaavaliacao ma = new Metaavaliacao();
                ma.setId(rs.getInt("id"));
                ma.setTitulo(rs.getString("titulo"));
                ma.setAno(rs.getInt("ano"));
                ma.setTipo(rs.getString("tipo"));
                ma.getAnexo1().setId(rs.getInt("anexo1"));
                ma.getAnexo2().setId(rs.getInt("anexo2"));
                ma.getAnexo3().setId(rs.getInt("anexo3"));
                metaavaliacaoes.add(ma);
            }

            conexao.closeConnection();

        } catch (SQLException ex) {
            conexao.closeConnection();
            Logger.getLogger(InstituicaoDao.class.getName()).log(Level.SEVERE, null, ex);
            return metaavaliacaoes;
        }

        return metaavaliacaoes;

    }
    
    
}
